<template>
  <el-row :gutter="20">
    <el-col :span="4" :xs="24">
      <ContentWrap class="h-1/1">
        <InventoryClassifyTree @node-click="handleClassifyNodeClick"/>
      </ContentWrap>
    </el-col>
    <el-col :span="20" :xs="24">
      <ContentWrap>
        <!-- 搜索工作栏 style="height:auto;align-items:stretch;" -->
        <el-form ref="queryFormRef" :inline="true" :model="queryParams" class="-mb-15px"
                 label-width="68px">
          <el-row>
            <el-col :span="5" :xs="24">
              <el-form-item label="存货编码" prop="code">
                <el-input
                  v-model="queryParams.code" placeholder="请输入存货编码" clearable
                  @keyup.enter="handleQuery"/>
              </el-form-item>
            </el-col>
            <el-col :span="5" :xs="24">
              <el-form-item label="存货名称" prop="name">
                <el-input
                  v-model="queryParams.name" placeholder="请输入存货名称" clearable
                  @keyup.enter="handleQuery"/>
              </el-form-item>
            </el-col>
            <el-col :span="5" :xs="24">
              <el-form-item label="规格型号" prop="specModel">
                <el-input
                  v-model="queryParams.specModel" placeholder="请输入规格型号" clearable
                  @keyup.enter="handleQuery"/>
              </el-form-item>
            </el-col>
            <el-col :span="5" :xs="24">
              <el-form-item label="品牌" prop="brand">
                <el-input
                  v-model="queryParams.brand" clearable
                  placeholder="请输入品牌" @keyup.enter="handleQuery"/>
              </el-form-item>
            </el-col>
            <el-col :span="4" :xs="24">
              <el-form-item label="批次管理" prop="isBatch">
                <el-select v-model="queryParams.isBatch" placeholder="" clearable
                           class="!w-100px">
                  <el-option
                    v-for="dict in getIntDictOptions(DICT_TYPE.WMS_INVENTORY_IS_BATCH)"
                    :key="dict.value" :label="dict.label" :value="dict.value"/>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12" :xs="24">
              <el-form-item>
                <el-button @click="handleQuery">
                  <Icon class="mr-5px" icon="ep:search"/>
                  搜索
                </el-button>
                <el-button @click="resetQuery">
                  <Icon class="mr-5px" icon="ep:refresh"/>
                  重置
                </el-button>
                <el-button v-hasPermi="['wms:inventory:create']" plain type="primary"
                           @click="openForm('create')">
                  <Icon class="mr-5px" icon="ep:plus"/>
                  新增
                </el-button>
                <el-button v-hasPermi="['wms:inventory:export']" :loading="exportLoading" plain type="success"
                           @click="handleExport">
                  <Icon class="mr-5px" icon="ep:download"/>
                  导出
                </el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </ContentWrap>
      <!-- 列表 -->
      <ContentWrap>
        <el-table
          v-loading="loading" :data="list" :show-overflow-tooltip="true" :stripe="true"
          style="height: calc(80vh - 50px - 50px - 35px - 25px)">
          <el-table-column align="center" label="存货编码" prop="code"/>
          <el-table-column align="center" label="存货名称" prop="name"/>
          <el-table-column align="center" label="规格型号" prop="specModel"/>
          <el-table-column align="center" label="存货分类" prop="classifyName"/>
          <el-table-column align="center" label="计量单位" prop="unitName"/>
          <el-table-column align="center" label="税率" prop="taxRate">
            <template #default="scope">
              <dict-tag :type="DICT_TYPE.WMS_TAX_RATE" :value="scope.row.taxRate"/>
            </template>
          </el-table-column>
          <el-table-column align="center" label="批次管理" prop="isBatch">
            <template #default="scope">
              <dict-tag :type="DICT_TYPE.WMS_INVENTORY_IS_BATCH" :value="scope.row.isBatch"/>
            </template>
          </el-table-column>
          <el-table-column align="center" label="品牌" prop="brand"/>
          <el-table-column :formatter="dateFormatter" align="center" label="创建时间" prop="createTime"
                           width="180px"/>
          <el-table-column align="center" label="操作" min-width="120px">
            <template #default="scope">
              <el-button v-hasPermi="['wms:inventory:update']" link type="primary"
                         @click="openForm('update', scope.row.id)">
                编辑
              </el-button>
              <el-button v-hasPermi="['wms:inventory:delete']" link type="danger"
                         @click="handleDelete(scope.row.id)">
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <Pagination v-model:limit="queryParams.pageSize" v-model:page="queryParams.pageNo"
                    :total="total" @pagination="getList"/>
      </ContentWrap>
    </el-col>
  </el-row>

  <!-- 表单弹窗：添加/修改 -->
  <InventoryForm ref="formRef" @success="getList"/>
</template>

<script lang="ts" setup>
import {DICT_TYPE, getIntDictOptions} from '@/utils/dict'
import {dateFormatter} from '@/utils/formatTime'
import download from '@/utils/download'
import {InventoryApi, InventoryVO} from '@/api/wms/inventory'
import InventoryForm from './InventoryForm.vue'
import InventoryClassifyTree from "./InventoryClassifyTree.vue";

/** 存货档案 列表 */
defineOptions({name: 'Inventory'})

const message = useMessage() // 消息弹窗
const {t} = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<InventoryVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  code: undefined,
  name: undefined,
  specModel: undefined,
  classifyId: undefined,
  isBatch: undefined,
  brand: undefined,
  createTime: [],
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await InventoryApi.getInventoryPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 处理分类树被点击 */
const handleClassifyNodeClick = async (row) => {
  queryParams.classifyId = row.id
  await getList()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  if (!id && queryParams.classifyId) {
    formRef.value.open(type, queryParams.classifyId)
  } else {
    formRef.value.open(type, id)
  }
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await InventoryApi.deleteInventory(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {
  }
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await InventoryApi.exportInventory(queryParams)
    download.excel(data, '存货档案.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
